<?php


namespace app\system\service;

class AdminService
{
	public static function getlist($options)
	{
		$role_id_name = \think\Db::name("system_admin_role")->column("name", "id");
		$where = [];
		if (isset($options["role_id"]) && $options["role_id"]) {
			$where[] = ["role_id", "=", $options["role_id"]];
		}
		if (isset($options["name"]) && $options["name"]) {
			$where[] = ["name", "like", "%{$options["name"]}%"];
		}
		if (isset($options["account"]) && $options["account"]) {
			$where[] = ["account", "like", "%{$options["account"]}%"];
		}
		$admin_count = \think\Db::name("system_admin")->where($where)->count();
		$admin_lists = \think\Db::name("system_admin")->where($where)->page($options["page"], $options["limit"])->field(["id", "root", "name", "account", "role_id", "create_time", "login_time", "login_ip", "disable", "verify_login_ip"])->select();
		$pages = ceil($admin_count / $options["limit"]);
		foreach ($admin_lists as $k => $v) {
			if ($v["root"] == 1) {
				$admin_lists[$k]["role"] = "超级管理员";
			} else {
				$admin_lists[$k]["role"] = $role_id_name[$v["role_id"]];
			}
			$admin_lists[$k]["create_time_str"] = date("Y-m-d H:i:s", $v["create_time"]);
			$admin_lists[$k]["login_time"] = empty($v["login_time"]) ? "" : date("Y-m-d H:i:s", $v["login_time"]);
			$admin_lists[$k]["login_state"] = 1;
		}
		return ["code" => \app\system\model\Code::success, "data" => $admin_lists, "count" => $admin_count, "pages" => $pages];
	}
	public static function add($options)
	{
		\think\Db::startTrans();
		try {
			$query_id = \think\Db::name("system_admin")->where(["account" => $options["account"]])->value("id");
			if (!empty($query_id)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "该账号不可用请更换"];
			}
			$role = \think\Db::name("system_admin_role")->where(["id" => $options["role_id"]])->find();
			if (empty($role)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "角色不存在"];
			}
			$time = time();
			$password = GeneratePassword($options["password"]);
			$data = ["name" => $options["name"], "root" => 0, "account" => $options["account"], "password" => $password, "token" => md5(time()), "role_id" => $options["role_id"], "create_time" => $time, "disable" => $options["disable"]];
			$insert = \think\Db::name("system_admin")->insert($data);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function getinfo($options)
	{
		$data = \think\Db::name("system_admin")->where(["id" => $options["id"]])->field(["id", "root", "name", "account", "role_id", "create_time", "login_time", "login_ip", "disable", "verify_login_ip"])->find();
		if (empty($data)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "ID不存在"];
		}
		return ["code" => \app\system\model\Code::success, "data" => $data];
	}
	public static function edit($options)
	{
		\think\Db::startTrans();
		try {
			$admin = \think\Db::name("system_admin")->where(["id" => $options["id"]])->find();
			if (!$admin) {
				return ["code" => \app\system\model\Code::fail, "msg" => "不存在的管理员ID"];
			}
			if ($admin["root"] == 1) {
				return ["code" => \app\system\model\Code::fail, "msg" => "无法编辑超级管理员"];
			}
			if ($admin["id"] == $options["admin_info"]["id"]) {
				return ["code" => \app\system\model\Code::fail, "msg" => "无法编辑自己"];
			}
			$role = \think\Db::name("system_admin_role")->where(["id" => $options["role_id"]])->find();
			if (empty($role)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "角色不存在"];
			}
			$time = time();
			$data = ["name" => $options["name"], "role_id" => $options["role_id"], "update_time" => $time, "disable" => $options["disable"]];
			if (!empty($options["password"])) {
				$password = GeneratePassword($options["password"]);
				$data["password"] = $password;
				$data["token"] = md5($time);
			}
			\think\Db::name("system_admin")->where(["id" => $options["id"]])->update($data);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function del($options)
	{
		\think\Db::startTrans();
		try {
			$admin = \think\Db::name("system_admin")->where(["id" => $options["id"]])->find();
			if (!$admin) {
				return ["code" => \app\system\model\Code::fail, "msg" => "不存在的管理员ID"];
			}
			if ($admin["root"] == 1) {
				return ["code" => \app\system\model\Code::fail, "msg" => "无法删除超级管理员"];
			}
			if ($admin["id"] == $options["admin_info"]["id"]) {
				return ["code" => \app\system\model\Code::fail, "msg" => "您不能删除自己"];
			}
			\think\Db::name("system_admin")->where(["id" => $options["id"]])->delete();
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function revise_verify_login_ip($options)
	{
		\think\Db::startTrans();
		try {
			$admin = \think\Db::name("system_admin")->where(["id" => $options["id"]])->find();
			if (!$admin) {
				return ["code" => \app\system\model\Code::fail, "msg" => "不存在的管理员ID"];
			}
			if ($options["admin_info"]["root"] !== 1) {
				return ["code" => \app\system\model\Code::fail, "msg" => "此操作需要超级管理员权限"];
			}
			\think\Db::name("system_admin")->where(["id" => $options["id"]])->update(["verify_login_ip" => !$admin["verify_login_ip"]]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
}